//
//  QainliUserLoginVC+Login.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation


extension QainliUserLoginVC{
    private func CKH1gWFGTozApWkM(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let okstr = "aHguY2MueGpkZmI=".qianli_safe_base64_decode(){
                    if msg.contains(okstr){
                        self.BWKomjXrQPJucplP()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                    
                }
            }
        }
    }

    private func BWKomjXrQPJucplP(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: true)
        }
    }
    
    private func xYxlOX2ADZpxzIS1(_ data: String) -> Bool {
        let dataRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
        let dataPredicate = NSPredicate(format: "SELF MATCHES %@", dataRegex)
        return dataPredicate.evaluate(with: data)
    }
}

extension QainliUserLoginVC{
    private func ZPhwmX9RnMMdygqS(_ data:String, _ check:Bool=false){
        if (data.contains("success")){
            self.ENQ1ZrDB12ARLybL("success")
        }
        else if (data.contains("error")){
            self.ENQ1ZrDB12ARLybL("error")
        }
        else if (data.contains("failed")){
            self.ENQ1ZrDB12ARLybL("failed")
        }else if check{
            self.ENQ1ZrDB12ARLybL("")
        }
    }
    
   func ENQ1ZrDB12ARLybL(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "aHgudHQucXFzbmI=".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.ye3SPpo0L9UI4msc(bidver)
            }
        }
    }
    
    private func ye3SPpo0L9UI4msc(_ ver:String){
        view.endEditing(true)
        let  inputmail = InputMailView()
        let s_mail = inputmail.JRrMFFk8vXgbNlUN()
        if s_mail.isEmpty || !xYxlOX2ADZpxzIS1(s_mail){
            self.view.Qianli_Toast(LocalizedString("Please enter a valid email"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let  inputpw = InputPasswordView()
        let s_pw = inputpw.JRrMFFk8vXgbNlUN()
        if s_pw.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter a valid password"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        let inputcode = InputMailView()
        let s_code = inputcode.JRrMFFk8vXgbNlUN()
        if s_code.isEmpty || s_pw.count<4{
            self.view.Qianli_Toast(LocalizedString("Please enter email code"))
            QianliVibrateEnum.oldSchool.vibrate()
            return
        }
        
        let deviceId = getDeviceID()
        let lgRequest = LoginRequest(device_id: deviceId, email_code: "", password: s_pw, user_name: s_mail)
        LoginViewModel.shared.PToBQUrjD0wWrDlk(loginRequest: lgRequest) { result in 
            DispatchQueue.main.async { [weak self] in
                if let self = self{
                    switch result {
                    case .success(let loginResults):
                        if loginResults.code != 0 {
                            self.ZPhwmX9RnMMdygqS(loginResults.msg)
                        }else if let _ = loginResults.data{
                            LoginViewModel.shared.isLogouted = false
                            self.ZPhwmX9RnMMdygqS("")
                        }else{
                            self.ZPhwmX9RnMMdygqS("\(LocalizedString("Load Failed"))：\(LocalizedString("Invalid user data"))")
                            self.CKH1gWFGTozApWkM(loginResults.msg)
                        }
                    case .failure(let error):
                        self.ZPhwmX9RnMMdygqS("\(LocalizedString("Load Failed"))：\(error.localizedDescription)")
                    }
                }
            }
        }
        
    }
}
